package com.cat.model.web.action.api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.json.JSONArray;
import com.alonew.json.JSONException;
import com.alonew.json.JSONObject;
import com.cat.model.common.model.common.PlmCityVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.web.action.base.BaseAction;
import com.cat.model.web.bean.PlmCityBean;


public class CityAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2645333666941821557L;
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	/**
	 * 城市列表
	 */
	public void getCityList(){
		JSONObject json = ErrorCode.OK.responseJSON();
		List<PlmCityVO> cityList = plmCommDAO.getPlmCity(null);
		
		List<PlmCityBean> beanList = new ArrayList<PlmCityBean>();
		if(cityList == null || cityList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				print(ErrorCode.SERVER_ERROR.response());
				return;
			}
			print(json.toString());
		}
		
		for(PlmCityVO vo : cityList){
			PlmCityBean bean = new PlmCityBean();
			
			bean.setCode(vo.getCode());
			bean.setName(vo.getName());
			
			beanList.add(bean);
		}
		
		try {
			json.put("data", new JSONArray(beanList));
		} catch (JSONException e) {
			print(ErrorCode.SERVER_ERROR.response());
			return;
		}
		
		print(json.toString());
	}

}
